package com.lancoo.znbkxx.teacher.aibkTestCoach.aiPhotoPick;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lancoo.znbkxx.teacher.R;
import com.lancoo.znbkxx.teacher.widget.BamImageView;
import com.lancoo.znbkxx.teacher.widget.CustomRoundAngleImageView;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnItemClickListener;
import com.luck.picture.lib.tools.DateUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * auther: 王国龙
 * Date：
 * describe:
 */
public class photoAnalysisGridImageAdapter extends
        RecyclerView.Adapter<photoAnalysisGridImageAdapter.ViewHolder> {
    public static final String TAG = "PictureSelector";
    private LayoutInflater mInflater;
    private List<LocalMedia> list = new ArrayList<>();
    private int selectMax = 9;

    /**
     * 删除
     */
    public void delete(int position) {
        try {

            if (position != RecyclerView.NO_POSITION && list.size() > position) {
                list.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, list.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public photoAnalysisGridImageAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(List<LocalMedia> list) {
        this.list = list;
    }

    public List<LocalMedia> getData() {
        return list == null ? new ArrayList<>() : list;
    }

    public void remove(int position) {
        if (list != null) {
            list.remove(position);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        CustomRoundAngleImageView mIvPhoto;
        BamImageView mIvDelete;
        TextView mTvDuration;

        public ViewHolder(View view) {
            super(view);
            mIvPhoto = view.findViewById(R.id.IvPhoto);
            mIvDelete = view.findViewById(R.id.IvDelete);
            mTvDuration = view.findViewById(R.id.TvDuration);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.aibk_item_photopick_gv_filter_image,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        //少于8张，显示继续添加的图标
        viewHolder.mIvDelete.setVisibility(View.GONE);

        LocalMedia media = list.get(position);
        if (media == null
                || TextUtils.isEmpty(media.getPath())) {
            return;
        }
        int chooseModel = media.getChooseModel();
        String path;
        if (media.isCut() && !media.isCompressed()) {
            // 裁剪过
            path = media.getCutPath();
        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
            path = media.getCompressPath();
        } else {
            // 原图
            path = media.getPath();
        }

        Log.i(TAG, "原图地址::" + media.getPath());

        if (media.isCut()) {
            Log.i(TAG, "裁剪地址::" + media.getCutPath());
        }
        if (media.isCompressed()) {
            Log.i(TAG, "压缩地址::" + media.getCompressPath());
            Log.i(TAG, "压缩后文件大小::" + new File(media.getCompressPath()).length() / 1024 + "k");
        }
        if (!TextUtils.isEmpty(media.getAndroidQToPath())) {
            Log.i(TAG, "Android Q特有地址::" + media.getAndroidQToPath());
        }
        if (media.isOriginal()) {
            Log.i(TAG, "是否开启原图功能::" + true);
            Log.i(TAG, "开启原图功能后地址::" + media.getOriginalPath());
        }

        long duration = media.getDuration();
        if (duration > 0) {
            viewHolder.mTvDuration.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mTvDuration.setVisibility(View.GONE);
        }
        if (chooseModel == PictureMimeType.ofAudio()) {
            viewHolder.mTvDuration.setVisibility(View.VISIBLE);
            viewHolder.mTvDuration.setCompoundDrawablesRelativeWithIntrinsicBounds
                    (R.drawable.picture_icon_audio, 0, 0, 0);
        } else {
            viewHolder.mTvDuration.setCompoundDrawablesRelativeWithIntrinsicBounds
                    (R.drawable.picture_icon_video, 0, 0, 0);
        }
        viewHolder.mTvDuration.setText(DateUtils.formatDurationTime(duration));

        if (chooseModel == PictureMimeType.ofAudio()) {
            viewHolder.mIvPhoto.setImageResource(R.drawable.picture_audio_placeholder);
        } else {
            Glide.with(viewHolder.itemView.getContext())
                    .load(PictureMimeType.isContent(path) && !media.isCut() && !media.isCompressed() ? Uri.parse(path)
                            : path)
                    .centerCrop()
                    .placeholder(R.drawable.aibk_anim_loading)
                    .error(R.mipmap.aibk_icon_photo_broken)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(viewHolder.mIvPhoto);
        }
        //itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(v -> {
                int adapterPosition = viewHolder.getAdapterPosition();
                mItemClickListener.onItemClick(v, adapterPosition);
            });
        }

//        }
    }

    private OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener l) {
        this.mItemClickListener = l;
    }

}